package ru.vpitervip.app.client;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import ru.vpitervip.app.client.activity.MasterActivityMapper;
import ru.vpitervip.app.client.activity.SearchHandler;
import ru.vpitervip.app.client.place.FlatsPlace;
import ru.vpitervip.app.client.place.MainMenuItemPicker;
import ru.vpitervip.app.client.place.MainPlace;
import ru.vpitervip.app.client.place.MenuItemPlace;
import ru.vpitervip.app.client.request.AppRequestFactory;
import ru.vpitervip.app.client.style.AppResources;
import ru.vpitervip.app.client.ui.AppDesktopShell;


import ru.vpitervip.client.scaffold.request.RequestEvent;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.CachingActivityMapper;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.logging.client.LogConfiguration;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.place.shared.PlaceHistoryMapperWithFactory;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HasConstrainedValue;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.inject.Inject;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryLogHandler;
import com.google.web.bindery.requestfactory.shared.LoggingRequest;

public class DesktopApp extends App{
	
	
	private static final Logger log = Logger.getLogger(AppEntryPoint.class.getName());
	private final AppDesktopShell shell;
	private final AppRequestFactory requestFactory;
	private final EventBus eventBus;
	private final PlaceController placeController;
	private final MasterActivityMapper masterActivityMapper;
	
	
	

	@Inject
	public DesktopApp(AppDesktopShell shell, AppRequestFactory requestFactory, EventBus eventBus, PlaceController placeController, MasterActivityMapper masterActivityMapper) {
		this.shell = shell;
		this.requestFactory = requestFactory;
		this.eventBus = eventBus;
		this.placeController = placeController;
		this.masterActivityMapper = masterActivityMapper;
		
	}
	
	
	

	public void run() {
		/* Add handlers, setup activities */
		init();

		/* Hide the loading message */
		Element loading = Document.get().getElementById("loading");
		loading.getParentElement().removeChild(loading);

		/* And show the user the shell */
//		RootLayoutPanel.get().add(shell);
		RootPanel.get("site").add(shell);
	}

	private void init() {
		GWT.setUncaughtExceptionHandler(new GWT.UncaughtExceptionHandler() {
			public void onUncaughtException(Throwable e) {
				Window.alert("Error: " + e.getMessage());
				log.log(Level.SEVERE, e.getMessage(), e);
			}
		});

		if (LogConfiguration.loggingIsEnabled()) {
			// Add remote logging handler
			RequestFactoryLogHandler.LoggingRequestProvider provider = new RequestFactoryLogHandler.LoggingRequestProvider() {
				public LoggingRequest getLoggingRequest() {
					return requestFactory.loggingRequest();
				}
			};
			Logger.getLogger("").addHandler(new RequestFactoryLogHandler(provider, Level.WARNING, new ArrayList<String>()));
		}

//		RequestEvent.register(eventBus, new RequestEvent.Handler() {
//			// Only show loading status if a request isn't serviced in 250ms.
//			private static final int LOADING_TIMEOUT = 250;
//
//			public void onRequestEvent(RequestEvent requestEvent) {
//				if (requestEvent.getState() == RequestEvent.State.SENT) {
//					shell.getMole().showDelayed(LOADING_TIMEOUT);
//				} else {
//					shell.getMole().hide();
//				}
//			}
//		});

		CachingActivityMapper cached = new CachingActivityMapper(masterActivityMapper);
		final ActivityManager masterActivityManager = new ActivityManager(cached, eventBus);

		masterActivityManager.setDisplay(shell.getMasterPanel());

		
		MainMenuItemPicker menuItemPicker=new MainMenuItemPicker(placeController);
		HasConstrainedValue<MenuItemPlace> listPlacePickerView = shell.getPlacesBox();
		listPlacePickerView.setAcceptableValues(getMainMenuPlaces());
		menuItemPicker.register(eventBus, listPlacePickerView);
		
		
		SearchHandler searchHandler=new SearchHandler(shell.getSearchPanel(), requestFactory, placeController);
		
		
		placeController.goTo(new MainPlace("Главная"));
		
		/* Browser history integration */
//		ScaffoldPlaceHistoryMapper mapper = GWT.create(ScaffoldPlaceHistoryMapper.class);
//		mapper.setFactory(placeHistoryFactory);
//		PlaceHistoryHandler placeHistoryHandler = new PlaceHistoryHandler(mapper);
//		if (getTopPlaces().iterator().hasNext()) {
//			ProxyListPlace defaultPlace = getTopPlaces().iterator().next();
//			placeHistoryHandler.register(placeController, eventBus, defaultPlace);
//			placeHistoryHandler.handleCurrentHistory();
//		}
	}

}
